<?php
namespace app\api\controller\v1;
use app\common\service\LitestoreOrder as LitestoreOrderservice;
use app\common\service\LitestoreShopOrder as ShopOrderservice;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/15
 * Time: 9:57
 */
class Order extends  Base{
    protected $noNeedLogin = ['index','getUserInfo','ShopOrder'];
    protected $noNeedRight = '*';
    public $uid;
    public function __construct()
    {
        parent::__construct();
        $userinfo = $this->userinfo;
        $this->uid=$userinfo['id'];
    }

    /**
 * 我的订单
 */
    public function myOrder(){
        $LitestoreOrderservice = new LitestoreOrderservice();
        $uid = $this->uid;
        $offset = $this->request->request('offset');
        $offsets = $offset?$offset:0;
        $number = $this->request->request('limit');
        $pay_status = $this->request->request('pay_status');
        $limit = $number?$number:6;
        $limits = $offsets.','.$limit;
        $data = $LitestoreOrderservice->StoreOrderByList($uid,$limit,$pay_status);
        if(!$data){
            return json(array('code'=>0,"data"=>[],'msg'=>"暂无订单记录"));
        }
        $this->success("请求成功",$data);
    }

    /**
     * 商家订单
     */
    public function ShopOrder(){
        $data=array();
        $ShopOrderservice = new ShopOrderservice();
        $user=$this->auth->getUser();
        $uid = $user['id'];
        $offset = $this->request->request('offset');
        $offsets = $offset?$offset:0;
        $number = $this->request->request('limit');
        $pay_status = $this->request->request('status');
        $limit = $number?$number:10;
//        $limits = $offsets.','.$limit;

        $data = $ShopOrderservice->ShopOrderList($uid,$offsets,$limit,$pay_status);
        if(!$data){
            return json(array('code'=>0,"data"=>[],'msg'=>"暂无订单记录"));
        }
        $this->success("请求成功",$data);
    }

    /**
     * 评论接口
     */
    public function comment(){
        try{
            $user=$this->auth->getUser();
            $uid=$user['id'];
            $data['content'] = $this->request->request('content');
            $data['psoce'] = $this->request->request('psoce');
            if($data['psoce']>5){
                $data['psoce']=5;
            }
            $litestore_shop_id = $this->request->request('litestore_shop_id');
            $litestore_shoporder_id = $this->request->request('litestore_shoporder_id');

            $data['litestore_shop_id']=$litestore_shop_id;
            $data['litestore_shoporder_id']=$litestore_shoporder_id;
            $data['puser_id']=$uid;
            $data['createtime']=time();
            Db::name('litestore_shopcomment')->insert($data);
            $average_sum=Db::name('litestore_shopcomment')->where(['litestore_shop_id'=>$litestore_shop_id])->sum('psoce');
            $average_count=Db::name('litestore_shopcomment')->where(['litestore_shop_id'=>$litestore_shop_id])->count('psoce');
            $udata['average']=round ($average_sum/$average_count);
            Db::name('litestore_shop')->where(['id'=>$litestore_shop_id])->update($udata);
            $ud['comment_status']=1;
            Db::name('litestore_shoporder')->where(['shoporder_id'=>$litestore_shoporder_id])->update($ud);
        }catch (\Exception $e){
            $this->error('评论失败');
        }
        $this->success("评论成功");
    }
    /**
     * 获取代理
     */

     public function getproxy(){
         $data=array();
         try{
             $prox_pr=0;
             $prox_shop=0;
             $user=$this->auth->getUser();
             $uid=$user->id;
             $user_shop=Db::name('user')
                 ->where(['id'=>$uid,'server_type'=>array('in',array(1,2,3,5))])
                 ->count();


             $user_pr=Db::name('user')
                 ->where(['id'=>$uid,'is_agent'=>1])
                 ->count();


             if($user_shop>0){
                 $data['province']=1;
                 $data['city']=1;
                 $data['area']=1;
                 $data['regione']=1;
             }else{
                 $prox_shop=Db::name('proxylog')
                     ->where(['user_id'=>$uid,'server_type'=>array('in',array(1,2,3,5)),'status'=>array('in',array('0','1'))])
                     ->count();

                 if($prox_shop>0){
                     $data['province']=1;
                     $data['city']=1;
                     $data['area']=1;
                     $data['regione']=1;
                 }else{
                     $data['province']=0;
                     $data['city']=0;
                     $data['area']=0;
                     $data['regione']=0;
                 }
             }

             if($user_pr>0){
                 $data['is_agent']=1;
             }else{
                 $prox_pr=Db::name('proxylog')
                     ->where(['user_id'=>$uid,'is_agent'=>1,'status'=>array('in',array('0','1'))])
                     ->count();

                 if(!empty($prox_pr)){
                     $data['is_agent']=1;
                 }else{
                     $data['is_agent']=0;
                 }
             }






//            $user=$this->auth->getUser();
//             $province_count=Db::name('user')->where(['province'=>$user['province'],'server_type'=>array('eq',1)])->count();
//
//            if($province_count>0){
//                $provinceproxy_count=Db::name('proxylog')->where(['province'=>$data['province'],'server_type'=>array('eq',1),'status'=>array('eq',1)])->count();
//                if($provinceproxy_count>0){
//                    $data['province']=1;
//                }else{
//                    $data['province']=0;
//                }
//
//            }else{
//                $data['province']=0;
//            }
//
//             $city_count=Db::name('user')->where(['city'=>$user['city'],'server_type'=>array('eq',2)])->count();
//
//             if($city_count>0){
//                 $cityproxy_count=Db::name('proxylog')->where(['city'=>$user['city'],'server_type'=>array('eq',2),'status'=>array('eq',1)])->count();
//                 if($cityproxy_count>0){
//                     $data['city']=1;
//                 }else{
//                     $data['city']=0;
//                 }
//             }else{
//                 $data['city']=0;
//             }
//
//             $area_count=Db::name('user')->where(['area'=>$user['area'],'server_type'=>array('eq',3)])->count();
//             if($area_count>0){
//                 $areaproxy_count=Db::name('proxylog')->where(['area'=>$user['area'],'server_type'=>array('eq',3),'status'=>array('eq',1)])->count();
//                 if($areaproxy_count>0){
//                     $data['area']=1;
//                 }else{
//                     $data['area']=0;
//                 }
//             }else{
//                 $data['area']=0;
//             }
//
//
//             $area_data_count=Db::name('user')->where(['regione'=>$user['regione'],'server_type'=>array('eq',5)])->count();
//             if($area_data_count>0){
//                 $areaproxy_data_count=Db::name('proxylog')->where(['regione'=>$user['regione'],'server_type'=>array('eq',5),'status'=>array('eq',1)])->count();
//                 if($areaproxy_data_count>0){
//                     $data['regione']=1;
//                 }else{
//                     $data['regione']=0;
//                 }
//             }else{ $data['regione']=0;
//
//             }
//
//             if($user['is_agent']>1){
//                 $areaproxy_data_count=Db::name('proxylog')->where(['user_id'=>$user['id'],'is_agent'=>array('eq',1),'status'=>array('eq',1)])->count();
//                 if($areaproxy_data_count>0){
//                     $data['regione']=1;
//                 }else{
//                     $data['is_agent']=0;
//                 }
//
//             }else{
//                 $data['is_agent']=1;
//             }




         }catch (\Exception $e){
             $this->success($e->getMessage());
         }
         $this->success("成功",$data);
     }

}